package com.renjun.smweb.controller.NoticeController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.service.NoticeService.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by RenJun on 2017/2/21.
 * 公告管理M9
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 查询公告/公告列表
     * @author lrs
     * @param request params:{title:标题, userid:管理员id}
     * @param response data:{sortnum:优先级, mod:类型, title:标题, lasttime:发布时间, status:状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/queryNotice", method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M9-1", desc = "查询公告/公告列表")
    public JSONObject queryNotice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.queryNotice(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 添加公告
     * @author lrs
     * @param request params:{title:标题, msg:内容, mod:类型, lasttime:发布时间, fileid:文件id, sortnum:优先级}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/addNoticeForCity", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-2", desc = "添加市级公告")
    public JSONObject addNoticeForCity(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.addNoticeForCity(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    @RequestMapping(value = "/modifyNotice", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-3", desc = "修改公告")
    public JSONObject modifyNotice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.modifyNotice(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 删除公告
     * @author lrs
     * @param request params:{id:公告id}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/cancelNotice", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-4", desc = "删除公告")
    public JSONObject cancelNotice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.cancelNotice(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 查看公告
     * @author lrs
     * @param request params:{id:公告id}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/getNotice", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-5", desc = "查看公告")
    public JSONObject getNotice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.getNotice(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }


    /**
     * 添加区级公告
     * @author lrs
     * @param request params:{title:标题, msg:内容, lasttime:发布时间, fileid:文件id, sortnum:优先级}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/addNoticeForDistrict", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-6", desc = "添加区级公告")
    public JSONObject addNoticeForDistrict(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.addNoticeForDistrict(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }


    /**
     * 首页市级公告列表
     * @author lrs
     * @param request params:{title:标题, msg:内容, lasttime:发布时间, fileid:文件id, sortnum:优先级}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/queryHomeNotice", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-7", desc = "首页市级公告列表")
    public JSONObject queryHomeNotice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.queryHomeNotice(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }


    /**
     * 恢复公告
     * @author lrs
     * @param request params:{id:公告id}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/recoverNotice", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M9-8", desc = "恢复公告")
    public JSONObject recoverNotice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = noticeService.recoverNotice(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }
}
